﻿using FlexFlow.Base.Interface;
using FlexFlow.Model;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlexFlow.DataProvider.MongoDB
{
    /// <summary>
    /// 表示MongoDb工作流引擎表单提供器
    /// </summary>
    public class MongoDbWorkFlowFormProvider : MongoDBPhysicalModelProvider<WorkFlowForm>, IWorkFlowFormProvider
    {
        /// <summary>
        /// 创建一个新的工作流表单
        /// </summary>
        /// <returns>
        /// 创建的工作流表单
        /// </returns>
        public override WorkFlowForm Create()
        {
            return new WorkFlowForm()
            {
                CreateTime = DateTime.Now
            };
        }
        /// <summary>
        /// 重命名表单
        /// </summary>
        /// <param name="id">
        /// 表单ID
        /// </param>
        /// <param name="newName">
        /// 新名称
        /// </param>
        /// <returns>
        /// 一个表示操作是否成功的值
        /// </returns>
        public bool Rename(Guid id, string newName)
        {
            var updateDef = BuildUpdateDef(m => m.Name, newName);
            return Update(id, updateDef);
        }
        /// <summary>
        /// 取得指定用户创建的具有指定名称的指定类型的工作流表单
        /// </summary>
        /// <param name="creator">
        /// 创建者ID
        /// </param>
        /// <param name="name">
        /// 名称
        /// </param>
        /// <param name="temp">
        /// 表单模板ID
        /// </param>
        /// <returns>
        /// 取得的功能工作流表单实例
        /// </returns>
        public WorkFlowForm Get(string name, Guid temp, Guid creator)
        {
            IMongoCollection<WorkFlowForm> col = this.GetCollection();
            FilterDefinition<WorkFlowForm> filter = (FilterDefinition<WorkFlowForm>)(
                m => m.Name == name &&
                    m.Template == temp &&
                    m.Creator == creator);
            return col.FirstOrDefault(filter);
        }
        /// <summary>
        /// 更新指定ID对应的表单模板的内容
        /// </summary>
        /// <param name="frmId">
        /// 表单模板ID
        /// </param>
        /// <param name="content">
        /// 表单内容
        /// </param>
        /// <returns>
        /// 一个值，表示更新是否成功
        /// </returns>
        public bool UpdateContent(Guid frmId, Dictionary<string, Object> content)
        {
            UpdateDefinition<WorkFlowForm> upDef = this.BuildUpdateDef(m => m.Content, content);
            return this.Update(frmId, upDef);
        }
        /// <summary>
        /// 取得指定用户创建的并且名称包含指定字符的表单列表
        /// </summary>
        /// <param name="creator">
        /// 表单的创建者
        /// </param>
        /// <param name="nameLike">
        /// 要取得的表单名称中应包含的字符列表
        /// </param>
        /// <param name="limit">
        /// 返回的数据数量限制
        /// </param>
        /// <param name="skip">
        /// 返回的数据的起始位置
        /// </param>
        /// <returns>
        /// 表单列表
        /// </returns>
        public List<WorkFlowForm> GetUserForms(Guid creator, string nameLike = "", Guid? formTemp = null, int skip = 0, int limit = 10)
        {
            FindOptions<WorkFlowForm> findOpt = new FindOptions<WorkFlowForm>()
            {
                Skip = skip,
                Limit = limit
            };
            FilterDefinition<WorkFlowForm> filter = null;
            if (formTemp == null)
            {
                filter = (FilterDefinition<WorkFlowForm>)(m => m.Creator == creator);
            }
            else
            {
                filter = (FilterDefinition<WorkFlowForm>)(m => m.Creator == creator && m.Template == formTemp);
            }
            return this.GetCollection().QueryByName(nameLike, findOpt, filter);
        }
    }
}
